<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="btnaddBrand">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="批次" value="batch"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row ref="tableDataRef" max-height="535">
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="batch" label="批次"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="180" align="center" prop="package_name" label="应用"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="160" align="center" prop="class_name" label="类名"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="100" align="center" prop="x1" label="x1"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="100" align="center" prop="x2" label="x2"></el-table-column>
      <el-table-column v-if="colData[5].istrue" min-width="100" align="center" prop="y1" label="y1"></el-table-column>
      <el-table-column v-if="colData[6].istrue" min-width="100" align="center" prop="y2" label="y2"></el-table-column>
      <el-table-column v-if="colData[7].istrue" min-width="140" align="center" prop="update_time" label="更新时间">
        <template slot-scope="scope">{{scope.row.update_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[8] ? colData[8].istrue : false" min-width="180" align="center" prop="username" label="归属"></el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getappCoordinateById(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加dialog表单 -->
    <el-dialog title="应用坐标访问管控" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="30%" @close="addFormclose">
      <el-form :model="addForm" ref="ruleForm" :rules="rules" style="padding-top:0" class="dialog_form">
        <el-form-item label="批次:" label-width="80px" prop="batch">
          <el-select v-model="addForm.batch" placeholder="请选择批次" class="add_select" :disabled="!editSubjects" filterable>
            <el-option v-for="item in lotoptions" :key="item.id" :label="item.batch" :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用:" label-width="80px" prop="package_name" >
          <el-select v-model="addForm.package_name" placeholder="请选择应用" class="add_select" :disabled="!editSubjects" filterable>
            <el-option v-for="(item) in appOptions" :key="item.app_id" :label="item.app_name" :value="item.app_package"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类名:" label-width="80px" prop="class_name">
          <el-input v-model="addForm.class_name" class="add_select" placeholder="请输入类名"></el-input>
        </el-form-item>
        <el-form-item label="坐标:" label-width="80px" prop="desc">
          <div class="flex flex_bet felx_align">
            <div class="flex felx_align"><span style="width: 40px;">X1:</span>
              <el-input v-model="addForm.x1" class="add_select" placeholder="请输入X1"></el-input>
            </div>
            <div class="flex felx_align"><span style="width: 40px;">Y1:</span>
              <el-input v-model="addForm.y1" class="add_select" placeholder="请输入Y1"></el-input>
            </div>
          </div>
          <div class="flex flex_bet felx_align" style="margin-top:20px">
            <div class="flex felx_align"><span style="width: 40px;">X2:</span>
              <el-input v-model="addForm.x2" class="add_select" placeholder="请输入X2"></el-input>
            </div>
            <div class="flex felx_align"><span style="width: 40px;">Y2:</span>
              <el-input v-model="addForm.y2" class="add_select" placeholder="请输入Y2"></el-input>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" @click="getappCoordinateAdd('ruleForm')" size="mini" round>确 定</el-button>
        <el-button v-else type="primary" @click="getappCoordinateEdit('ruleForm')" size="mini" round>编辑</el-button>
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import { appCoordinateList, appCoordinateById, appCoordinateAdd, appCoordinateEdit, appCoordinateDelete, getAppName } from "@/api/deployment.js";
import tableMini from '@/utils/tableMini.js'
import deleteDialog from "@/components/deleteDialog";
import dynamicTable from "@/components/DynamicTable";
import { getBatchTwo } from "@/api/equipment.js";
export default {
  mixins: [tableMini],
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["批次", "应用", "类名", "x1", "x2", "y1", "y2", "更新时间", "归属"],
      tableData: [],
      select: "", //搜索条件
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), batch: undefined },
      //添加按钮控制
      dialogFormVisible: false,
      //编辑按钮控制
      editSubjects: true,
      //浏览器专题id
      subjectId: 0,
      lotoptions: [],
      appOptions: [],
      //添加表单
      addForm: { userId: localStorage.getItem("ID"), batch: '', package_name: "", class_name: "", x1: '', x2: '', y1: '', y2: '' },
      rules: {
        batch: [{ required: true, message: "请选择批次", trigger: "blur" }], package_name: [{ required: true, message: "请选择应用", trigger: "blur" }],
        class_name: [{ required: true, message: "请输入类名", trigger: "blur" }]
      },
    }
  },
  created() {
    this.getTabularData()
    this.getAppName()
    this.getBatchTwos()
  },
  methods: {
    getTabularData() {
      appCoordinateList(this.listQuery).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        this.tableData = []
        this.total = 0
      })
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
      appCoordinateDelete(params).then(res => {
        this.$message.success(res.msg)
        this.getTabularData()
      })
    },
    //根据ID查询
    getappCoordinateById(id) {
      this.editSubjects = false
      this.subjectId = id
      appCoordinateById({ id: id }).then(res => {
        this.dialogFormVisible = true
        this.addForm = res.data
      }).catch(err => { })
    },
    //修改
    getappCoordinateEdit(formName) {
      this.verdicyCoord()
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.userId = localStorage.getItem("ID")
          addForm.id = this.subjectId
          appCoordinateEdit(addForm).then(res => {
            this.dialogFormVisible = false
            this.$message.success(res.msg)
            this.getTabularData()
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    //增加应用霸屏
    getappCoordinateAdd(formName) {
      this.verdicyCoord()
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          appCoordinateAdd(addForm).then(res => {
            this.$message.success(res.msg)
            this.getTabularData()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    verdicyCoord() {
      if (this.addForm.x1 === this.addForm.x2 && this.addForm.x1 === this.addForm.y1 && this.addForm.x1 === this.addForm.y2) return this.$message('坐标不能输入一样，请重新输入')
    },
    //添加按钮事件
    btnaddBrand() {
      this.editSubjects = true
      this.dialogFormVisible = true
    },
    //根据ID获取批次列表
    getBatchTwos() {
      let data = { userId: localStorage.getItem("ID") }
      getBatchTwo(data).then(res => {
        this.lotoptions = res.data
      }).catch(err => {
        this.lotoptions = []
      })
    },
    //根据ID获取应用
    getAppName() {
      let data = { userId: localStorage.getItem("ID") }
      getAppName(data).then(res => {
        this.appOptions = res.data
      }).catch(err => {
        this.appOptions = []
      })
    },
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = this.$options.data().addForm
      })
    }
  }
}
</script>
